package hu.mapro.gwtui.server;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

import org.springframework.util.ClassUtils;

public class DataService {

	@PersistenceContext(unitName="persistenceUnitMapro")
	EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	public <T> List<T> list(String entityName) {
		Class<T> clazz;
		try {
			clazz = (Class<T>) ClassUtils.forName(entityName, ClassUtils.getDefaultClassLoader());
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
		
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<T> q = cb.createQuery(clazz);
		q.from(clazz);
		return entityManager.createQuery(q).getResultList();
	}
	
}
